﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections.Specialized;
using System.Configuration;

namespace WEALTH
{
    public partial class MemberLogin : ClientBasePage
    {
       
        private ExcuteDao dao = new ExcuteDao();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[SessionKeys.CURRENT_MEMBER] != null)
            {
                Response.Redirect("MemberDetail.aspx",true);            }
           
        }

        /// <summary>
        /// 登录按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void But_Login_Click(object sender, EventArgs e)
        {
            //string strtest = AppFunction.Encrypt(txt_LPassWord.Text);

            //txt_LUserName.Text = strtest;


            if (!string.IsNullOrEmpty(txt_LUserName.Text) && !string.IsNullOrEmpty(txt_LPassWord.Text))
            {
                string strusername = SpecialCharHandler.HtmlEncode(txt_LUserName.Text);
                string strpassword = txt_LPassWord.Text;

                DataSet dataset = dao.Get_LoginMember(strusername, AppFunction.Encrypt(strpassword));

                if (dataset == null || dataset.Tables.Count == 0
                    || dataset.Tables[0].Rows.Count == 0)
                {
                    ShowMsg("用户名或密码错误，请确认！", 1);
                    return;
                }

                if ("0".Equals(dataset.Tables[0].Rows[0]["M_Status"].ToString()))
                {
                    ShowMsg("用户未激活，请激活或者联系客服！", 1);
                    return;
                }
                putUserInSession(dataset);
                ClearText();
                ShowMsg("恭喜你，登陆成功！", 0);
                //登陆成功跳转页面
                Response.Redirect("MemberDetail.aspx", true);
            }
        }

        /// <summary>
        /// 将当前用户信息存入Session
        /// </summary>
        /// <param name="dataset">DataSet</param>
        private void putUserInSession(DataSet dataset)
        {
            MemberCurrent currentMember = new MemberCurrent();
            DataRow row = dataset.Tables[0].Rows[0];
            currentMember.Member_ID = Convert.ToInt32(row["M_ID"]);
            currentMember.M_Name = SpecialCharHandler.HtmlDiscode(row["M_Name"].ToString());
            currentMember.M_Phone = row["M_Phone"].ToString();
            currentMember.M_Email = row["M_Email"].ToString();
            currentMember.M_RealName = row["M_RealName"].ToString();
            currentMember.M_Idcard = row["M_Idcard"].ToString();
            currentMember.M_Number = Convert.ToInt32(row["M_Number"]);
            currentMember.M_IdcardType = row["M_IdcardType"].ToString();
            Session[SessionKeys.CURRENT_MEMBER] = currentMember;

        }

        private void ClearText()
        {
            txt_LUserName.Text = string.Empty;
            txt_LPassWord.Text = string.Empty;

            txt_UserName.Text = string.Empty;
            txt_Email.Text = string.Empty;
            txt_Phone.Text = string.Empty;
            txt_RealName.Text = string.Empty;
            txt_PassWord.Text = string.Empty;
            txt_ComPassWord.Text = string.Empty;
            txt_IDCard.Text = string.Empty;
            txt_Code.Text = string.Empty;

        }
        /// <summary>
        /// 用户注册按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void But_Region_Click(object sender, EventArgs e)
        {
            if (CheckRegData())
            {
                MemberCurrent currentMember = new MemberCurrent();

                currentMember.M_Name = SpecialCharHandler.HtmlEncode(txt_UserName.Text);
                currentMember.M_Password = AppFunction.Encrypt(txt_PassWord.Text);
                currentMember.M_Phone = txt_Phone.Text;
                currentMember.M_Email = txt_Email.Text;
                currentMember.M_RealName = txt_RealName.Text;
                currentMember.M_Idcard = txt_IDCard.Text;
                currentMember.M_Number = 0;
                currentMember.M_IdcardType = ddlidcardType.SelectedItem.Text;

                Random rand = new Random();
                string strVailecode = "";
                for (int nI = 0; nI < 4; nI++)
                {
                    strVailecode = strVailecode + rand.Next(9).ToString();
                }

                currentMember.M_Vailecode = strVailecode;

                Session[SessionKeys.REG_MEMBER] = currentMember;

                if (dao.Insert_Member(currentMember) < 0)
                {
                    NameValueCollection nvc = new NameValueCollection();
                    nvc.Add("@Name", currentMember.M_Name);
                    nvc.Add("@Vailecode", currentMember.M_Vailecode);

                    if (MailService.MailhtmlSender(new string[] { currentMember.M_Email }, 1, nvc))
                    {
                        //跳转页面到注册验证码
                        Response.Redirect("MemberRegister.aspx?p=" + AppFunction.Encrypt(currentMember.M_Name) + "&mail=" + currentMember.M_Email, true);
                    }
                    else
                    {
                        ShowMsg("动态验证码发送失败，请联系客服人员。", 1);
                    }
                }
            }
        }

        private bool CheckRegData()
        {
            if (!txt_Code.Text.Equals(Session["ViewCode"]))
            {
                ShowMsg("验证码不正确，请重新输入！", 1);
                txt_Code.Focus();
                return false;
            }

            if (ddlidcardType.SelectedValue.Equals("0"))
            {
                if (!AppFunction.CheckIDCard(txt_IDCard.Text))
                {
                    ShowMsg("身份证号不正确，请确认！", 1);
                    txt_IDCard.Focus();
                    return false;
                }
            }

            if (dao.Get_MemberByName(SpecialCharHandler.HtmlEncode(txt_UserName.Text)) > 0)
            {
                ShowMsg("用户已经存在！", 1);
                txt_UserName.Focus();
                return false;
            }
            return true;
        }
    }
}